<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >


    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol"  label="商圈">
            <a-select allowClear v-model="queryParam.companyId">
              <a-select-option v-for="item in companys" :key="item.id">{{item.name}}</a-select-option>
            </a-select>
          </a-form-item>

        <a-form-item v-if="type==1||type==0" :labelCol="labelCol" :wrapperCol="wrapperCol" label="积分">
          <a-input-number style="width: 100%" :min="0" placeholder="请输入积分"
                          v-decorator="[ 'totalFee', {rules: [{ required: true, message: '请输入积分' }]}]"
          ></a-input-number>
        </a-form-item>
        <a-form-item v-if="type==2" :labelCol="labelCol" :wrapperCol="wrapperCol" label="注意" style="color: red">
            该操作是清零选中商圈的所有会员的积分
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-textarea placeholder="备注" v-decorator="[ 'remark', {rules: []}]"
          ></a-textarea>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {dealZeroPoints } from '../../../api/memberService/member'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import { selectAll } from '../../../api/companyService/company'

  export default {
    components: { ATextarea },
    //--############  data() ###############-->
    data() {
      return {
        // 查询参数
        queryParam: {},

        companys:[],


        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,
        openid: '',
        type: '',
        keys: []
      }
    },
    //--############  data() ###############-->

    created() {
      selectAll().then(res => {
        this.companys = res.result
      })
    },

    //--############  methods() ###############-->
    methods: {
      edit(record, e, keys) {
        this.type = e
        this.isDisabled = true
        this.visible = true
        if (undefined == record) {
        } else {
          this.mdl = Object.assign({}, record)
          this.openid = record.openid
        }
        if (e == 2) {
          //this.keys = keys
        }
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        if(this.queryParam.companyId==null){
          this.$message.warn('请先选择商圈！')
          return
        }
        this.keys=this.queryParam.companyId

        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            // values.id = this.mdl.id
            values.openId = _this.openid
            _this.confirmLoading = true

            /*类型为0，调用积分兑换接口*/
            if (this.type == 0) {
              save(values).then((res) => {
                if (res.code === 200) {
                  _this.$message.success('保存成功')
                  _this.$emit('ok')
                  _this.close()
                } else {
                  this.$notification.error({
                    message: '错误',
                    description: res.message,
                    duration: 4
                  })
                }
              }).catch(() => {
                // Do something
              }).finally(() => {
                _this.confirmLoading = false
              })
            } else if (this.type == 1) { //类型是1，调用积分补录接口
              supplyPoints(values).then((res) => {
                if (res.code === 200) {
                  _this.$message.success('保存成功')
                  _this.$emit('ok')
                  _this.close()
                } else {
                  this.$notification.error({
                    message: '错误',
                    description: res.message,
                    duration: 4
                  })
                }
              }).catch(() => {
                // Do something
              }).finally(() => {
                _this.confirmLoading = false
              })
            } else if (this.type == 2) {
              values.companyId = this.keys
              dealZeroPoints(values).then((res) => {
                if (res.code === 200) {
                  _this.$message.success('保存成功')
                  _this.$emit('ok')
                  _this.close()
                } else {
                  this.$notification.error({
                    message: '错误',
                    description: res.message,
                    duration: 4
                  })
                }
              }).catch(() => {
                // Do something
              }).finally(() => {
                _this.confirmLoading = false
              })
            }
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
